package com.sunclouder.cactus.common.api.response;

import lombok.Getter;

import java.io.Serializable;

/**
 * 这个异常类是用来给 http/rpc 接口使用，附带 response code 的
 * <p>
 * 闲杂代码请勿使用
 * 哪怕你 throws a RuntimeException 都行
 */
@Getter
public class ResponseException extends RuntimeException implements Serializable {

    public static final long serialVersionUID = 1L;

    private ResponseCode code;

    /**
     * message 字段用于系统内部的错误信息，不应该返回给前端
     * extraArgs 作为自定义的错误信息，用于替换 code.getMessage() 中的占位符，返回给前端
     */
    private Object[] extraArgs;

    public ResponseException() {
    }

    public ResponseException(ResponseCode code, String message, Object... extraArgs) {
        super(message);
        this.code = code;
        this.extraArgs = extraArgs;
    }

    public ResponseException(ResponseCode code, String message) {
        super(message);
        this.code = code;
        this.extraArgs = null;
    }

    public ResponseException(ResponseCode code) {
        super(code.getMessage());
        this.code = code;
        this.extraArgs = null;
    }

    public ResponseCode getCode() {
        return code;
    }
}
